<template>
	<m-cpage :list="formList" @submit="submit"></m-cpage>
</template>

<script>
	export default {
		name:'forgetPwd',
		data() {
			return {
				isFirst:true,
				formList:[],
				formList1:[
					{
						label:'请输入常用邮箱',
						placeholder:'请输入',
						tag:'email',
						type:'email',
						value:'',
						rules:{
							type: 'string',
							required: true,
							message: '请输入正确的邮箱',
							trigger: ['blur', 'change'],
							validator: (rule, value, callback) => {
								// 上面有说，返回true表示校验通过，返回false表示不通过
								// uni.$u.test.mobile()就是返回true或者false的
								return uni.$u.test.email(value);
							},
						},
					},
					{
						label:'填写邮箱验证码',
						placeholder:'输入验证码',
						tag:'code',
						type:'email',
						ptag:'email',
						icon:'send',
						isMobile:true,
						text:'发送验证码',
						cmd:'send_email_code',
						request_type:"2",
						rules:{
							type: 'string',
							required: true,
							message: '请输入正确的邮箱验证码',
						},
					},
					{
						button:true,
						disabled:false,
						submit:true,
						func:'submit',
						type:'',
						btn:'确定',
						btnStyle:{
							color:'#fff',fontSize:'14px',
							background:'#16b7ff',borderRadius:'5rpx',
						},
						classes:'noBorder pt50'
					},{
						button:true,
						disabled:false,
						func:'goBack',
						type:'text',
						btn:'返回',
						classes:'noBorder pb70',
						btnStyle:{
							color:'#26bbff',fontSize:'14px'
						},
					},{
						txt:true,
						classes:'noBorder ',
					}
				],
				formList2:[
					{
						label:'请输入绑定手机号码',
						placeholder:'请输入手机号码',
						tag:'phone',
						type:'phone',
						max:11,
						rules:{
							type: 'string',
							required: true,
							message: '请输入正确的手机号码',
							trigger: ['blur', 'change'],
							validator: (rule, value, callback) => {
								// 上面有说，返回true表示校验通过，返回false表示不通过
								// uni.$u.test.mobile()就是返回true或者false的
								return uni.$u.test.mobile(value);
							},
						},
					},
					{
						label:'填写验证码',
						placeholder:'输入验证码',
						tag:'code',
						type:'mobile',
						ptag:'phone',
						icon:'send',
						isMobile:true,
						cmd:'send_phone_code',
						request_type:"2",
						text:'发送验证码',
						rules:{
							type: 'string',
							required: true,
						},
					},
					{
						button:true,
						disabled:false,
						submit:true,
						func:'submit',
						type:'',
						btn:'下一步',
						btnStyle:{
							color:'#fff',fontSize:'16px',
							background:'#16b7ff',borderRadius:'5rpx',
						},
						classes:'noBorder pt50'
					},{
						button:true,
						disabled:false,
						func:'goBack',
						type:'text',
						btn:'返回',
						classes:'noBorder pb70',
						btnStyle:{
							color:'#26bbff',fontSize:'16px'
						},
					},
				],
			};
		},
		onReady() {
			
		},
		onLoad(options) {
			uni.setNavigationBarTitle({
				title:'修改验证' +'-宅小职'
			})
			if(options.setting){
				if(this.loginInfo.email){//有邮箱
					this.setEmailList()
				}else{
					this.setPhoneList()
				}
			}else{
				this.formList1[0].label= '账号邮箱'
				this.formList1[0].value= this.loginInfo.email;
				this.formList1[2].btn= '下一步';
				this.formList1 = this.formList1.slice(0,4)
				this.formList = this.formList1
			}
			
		},
		methods:{
			setEmailList(){
				this.formList1 = this.formList1.slice(0,5)
				this.formList1[0].classes= "bgee"
				this.formList1[0].disabled= true
				this.formList1[0].label= '账号邮箱'
				this.formList1[0].value= this.loginInfo.email;
				this.formList1[2].btn= '下一步';
				this.formList = this.formList1
			},
			setPhoneList(){
				this.formList2 = this.formList2.slice(0,4)
				this.formList2[0].disabled= true
				this.formList2[0].classes= "bgee"
				this.formList2[0].label= '账号手机'
				this.formList2[0].value= this.loginInfo.phone;
				this.formList2[2].btn= '下一步';
				this.formList = this.formList2
			},
			submit(val){
				let par = {}
				let cmd = 'reset_pwd_email_confirm_code'
				if(this.isFirst){
					par = {
						email:''
					}
				}else{
					par = {
						phone:''
					}
					cmd = 'reset_pwd_phone_confirm_code'
				}
				this.$api[cmd](val).then(res => {
					if(res.code == 200){
						this.utils.goPath('/pages/forgetPwd/forgetPwd',{
							...val,...this.options,
						},'redirectTo')
					}else{
						uni.showToast({
							title:res.message,
							icon:'none'
						})
						if(this.isFirst && this.loginInfo.phone){
							this.setPhoneList()
						}
						
					}
				})
			}
		}
	}
</script>

<style lang="scss">

</style>
